home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-03-09 | 4.8 KB | 157 lines | [TEXT/KAHL] |
- /**********************************************************************************
-
- NOTE: This file won't compile as-is, though each sample has been compiled on its
- own. The code here has been extracted from the article simply for your convenience,
- so you won't have to type it in. Take what you need, and adapt it for your own
- devious purposes.
-
- ***********************************************************************************/
-
-
- //--------------------------------------
- // An event loop that's two-byte savvy
-
- // Globals
- unsigned short gCharBuf; // Buffer that holds our (possibly two-byte)
- // character
- Boolean gNeed2ndByte; // Flag that tells us we're waiting for the
- // second byte of a two-byte character
-
- void EventLoop(void)
- {
- EventRecord event; // The current event
- short cbResult; // The results of our CharByte call
- unsigned char oneByte; // Single byte extracted from event
- Boolean processChar; // Whether we should send our application
- // a key message
-
- if (WaitNextEvent(everyEvent, &event, SleepTime(), nil)) {
- switch (event.what) {
- //. . .
- case keyDown:
- case autoKey:
- //. . .
- // Your code checks for Command-key equivalents here.
- //. . .
- processChar = false;
- oneByte = (event.message & charCodeMask);
- if (gNeed2ndByte) {
- // We're expecting the second byte of a two-byte character.
- // So OR the byte into the low byte of our accumulated
- // two-byte character.
- gCharBuf = (gCharBuf << 8) | oneByte;
- cbResult = CharByte((Ptr)&gCharBuf, 1);
- if (cbResult == smLastByte)
- processChar = true;
- gNeed2ndByte = false;
- } else {
- // We're not expecting anything in particular. We
- // might get a one-byte character, or we might get the
- // first byte of a two-byte character.
- gCharBuf = oneByte;
- cbResult = CharByte((Ptr)&gCharBuf, 1);
- if (cbResult == smFirstByte)
- gNeed2ndByte = true;
- else if (cbResult == smSingleByte)
- processChar = true;
- }
-
- // Now possibly send the typed character to the rest of the
- // application.
- if (processChar)
- AppKey(gCharBuf);
- break;
-
- // case . . .
- }
- }
- }
-
- //--------------------------------------
- // Getting the current date and time
- // into Pascal strings
-
- #define kWantSeconds true // For IUTimeString
- #define kNoSeconds false
-
- unsigned long secs;
- Str255 theDate, theTime;
-
- // Get the current date and time into Pascal strings.
- GetDateTime(&secs);
- IUDateString(secs, shortDate, theDate);
- IUTimeString(secs, kNoSeconds, theTime);
-
-
- //--------------------------------------
- // Formatting a number with FormatX2Str
-
- #define kFormatStrs 128 // resource id of a STR# resource with
- // that contains format strings
-
- OSErr FormatANum(short theFormat, extended theNum, Str255 theString)
- {
- NItl4Handle itl4;
- OSErr err;
- NumberParts numberParts;
- Str255 textFormatStr; // "Textual" number format spec
- NumFormatString formatStr; // Opaque number format
-
- // Load the 'itl4' and copy the NumberParts record out of it.
- itl4 = (NItl4Handle)IUGetIntl(4);
- if (itl4 == nil)
- return resNotFound;
- numberParts = *(NumberParts *)((char *)*itl4 +
- (*itl4)->defPartsOffset);
- // Get the format string, convert it to a NumFormatString, and then
- // use it to format the input number.
- GetIndString(textFormatStr, kFormatStrs, theFormat);
- err = Str2Format(textFormatStr, &numberParts, &formatStr);
- if (err != noErr)
- return err;
- err = FormatX2Str(theNum, &formatStr, &numberParts, theString);
- return err;
- }
-
- //--------------------------------------
- // Formatting a currency value.
- // Uses the routine above.
-
- OSErr FormatCurrency(extended theNum, Str255 theString)
- {
- Intl0Hndl itl0;
- OSErr err;
- Str255 currencySymbol, formattedValue;
-
- // First, format the number like this: ##,###.00. FormatX2Str will
- // replace the "," and "." separators appropriately for the font
- // script.
- err = FormatANum(kCurrencyFormat, theNum, formattedValue);
- if (err != noErr)
- return err;
-
- // Get the currency symbol from the 'itl0' resource. The currency
- // symbol is stored as up to three bytes. If any of the bytes aren't
- // used they're set to zero. So, we use strncpy to copy out the
- // currency symbol as a C string and forcibly terminate it in case it's
- // three bytes long.
- itl0 = (Intl0Hndl)IUGetIntl(0);
- if (itl0 == nil)
- return resNotFound;
- strncpy(currencySymbol, &(*itl0)->currSym1, 3);
- currencySymbol[3] = 0x00;
- c2pstr(currencySymbol);
-
- // Now put the currency symbol and the formatted value together
- // according to the currency symbol position.
- if ((*itl0)->currFmt & currSymLead) {
- StringCopy(theString, currencySymbol);
- StringAppend(theString, formattedValue);
- } else {
- StringCopy(theString, formattedValue);
- StringAppend(theString, currencySymbol);
- }
- return noErr;
- }
-
-